package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.College;
import com.ruoyi.system.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/system/college")
public class CollegeController extends BaseController {

    @Autowired
    private CollegeService collegeService;

    @PreAuthorize("@ss.hasPermi('system:college:list')")
    @GetMapping("/list")
    public TableDataInfo list(College college)
    {
        startPage();
        List<College> list = collegeService.selectCollegeList(college);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('system:college:list')")
    @GetMapping("/list/{name}")
    public TableDataInfo listName(@PathVariable String name)
    {
        startPage();
        List<College> collegeList = collegeService.selectName(name);
        return getDataTable(collegeList);
    }
    @PreAuthorize("@ss.hasPermi('system:college:query')")
    @GetMapping(value = "/{collageId}")
    public AjaxResult getInfo(@PathVariable Long collageId)
    {
        return AjaxResult.success(collegeService.selectCollegeByCollegeId(collageId));
    }

    @PreAuthorize("@ss.hasPermi('system:college:add')")
    @Log(title = "学院管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody College college)
    {
        college.setCreateBy(getUsername());
        return toAjax(collegeService.insertCollege(college));
    }

    @PreAuthorize("@ss.hasPermi('system:college:edit')")
    @Log(title = "学院管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody College college)
    {
        college.setUpdateBy(getUsername());
        return AjaxResult.success(collegeService.updateCollege(college));
    }

    @PreAuthorize("@ss.hasPermi('system:college:remove')")
    @Log(title = "学院管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{collageIds}")
    public AjaxResult remove(@PathVariable Long[] collageIds)
    {
        return toAjax(collegeService.deleteCollegeByCollegeIds(collageIds));
    }
    @PreAuthorize("@ss.hasPermi('system:college:query')")
    @GetMapping("/find/{name}")
    public TableDataInfo findByCollegeName(@PathVariable String name){
        List<College> colleges = collegeService.selectCollegeByCollegeName(name);
        return getDataTable(colleges);
    }


}
